var table;
// 加载元素控件
layui.use(['jquery', 'element', 'layer', 'laydate', 'form','table'], function () {
    $ = layui.jquery, element = layui.element, layer = layui.layer,form = layui.form,table=layui.table,laydate=layui.laydate;
    //获取 选中楼宇id
    var BUILDING_ID=QueryUtils.GetQueryString("activebuildingID");
    var BUSINESS_ID=QueryUtils.GetQueryString("activebusinessID");
    //加载待审核合同
    var data = {};
    data.BUILDING_ID=BUILDING_ID;
    data.BUSINESS_ID=BUSINESS_ID;
    element.on('tab(contract_category)', function (data1) {
        tabselect = $(this).data("con");
        switch (tabselect){
            case 0:
                table.render({
                    elem: '#contract'
                    , url: '/contract/contractingShenHe'
                    , where:data
                    , cols: [[
                        {field: 'BUILDING_NAME', event: 'setSign', title: '楼宇名称', width: 200}
                        , {field: 'ROOM_NUM', event: 'setSign', title: '项目编号', width: 120}
                        , {field: 'COMPANY', event: 'setSign', title: '承租方', width: 230}
                        , {field: 'ROOM_AREA', event: 'setSign', title: '面积(㎡)', width: 120}
                        , {field: 'rent', event: 'setSign', title: '租金单价', width: 120}
                        , {field: 'con_hr_date', event: 'setSign', title: '计租日', width: 120}
                        , {field: 'con_end_date', event: 'setSign', title: '结束日', width: 120}
                        , {field: 'CHECK_STATE', event: 'setSign', title: '状态', width: 120}
                        , {field: 'NAME', event: 'setSign', title: '提交人', width: 120}
                        , {field: 'create_date', event: 'setSign', title: '提交时间', width: 180}
                        , {fixed: 'right', width: 150, align: 'center', title: '操作', toolbar: '#shenhebar'}

                    ]]
                    , id: 'contractID'
                    , page: true
                    , height: '470'
                    , limit: '10'
                });
            case 1:
                table.render({
                    elem: '#contractyishenhe'
                    , url: '/contract/contractingYiShenHe'
                    , where:data
                    , cols: [[
                        {field: 'BUILDING_NAME', event: 'setSign', title: '楼宇名称', width: 200}
                        , {field: 'ROOM_NUM', event: 'setSign', title: '项目编号', width: 120}
                        , {field: 'COMPANY', event: 'setSign', title: '承租方', width: 230}
                        , {field: 'ROOM_AREA', event: 'setSign', title: '面积(㎡)', width: 120}
                        , {field: 'rent', event: 'setSign', title: '租金单价', width: 120}
                        , {field: 'con_hr_date', event: 'setSign', title: '计租日', width: 120}
                        , {field: 'con_end_date', event: 'setSign', title: '结束日', width: 120}
                        , {field: 'CHECK_STATE', event: 'setSign', title: '状态', width: 120}
                        , {field: 'NAME', event: 'setSign', title: '提交人', width: 120}
                        , {field: 'create_date', event: 'setSign', title: '提交时间', width: 180}
                        , {fixed: 'right', width: 160, align: 'center', title: '操作', toolbar: '#yishenhebar'}

                    ]]
                    , id: 'YIshenheID'
                    , page: true
                    , height: '470'
                    , limit: '10'
                });
        }
    });
    table.render({
        elem: '#contract'
        , url: '/contract/contractingShenHe'
        , where:data
        , cols: [[
            {field: 'BUILDING_NAME', event: 'setSign', title: '楼宇名称', width: 200}
            , {field: 'ROOM_NUM', event: 'setSign', title: '项目编号', width: 120}
            , {field: 'COMPANY', event: 'setSign', title: '承租方', width: 230}
            , {field: 'ROOM_AREA', event: 'setSign', title: '面积(㎡)', width: 120}
            , {field: 'rent', event: 'setSign', title: '租金单价', width: 120}
            , {field: 'con_hr_date', event: 'setSign', title: '计租日', width: 120}
            , {field: 'con_end_date', event: 'setSign', title: '结束日', width: 120}
            , {field: 'CHECK_STATE', event: 'setSign', title: '状态', width: 120}
            , {field: 'NAME', event: 'setSign', title: '提交人', width: 120}
            , {field: 'create_date', event: 'setSign', title: '提交时间', width: 180}
            , {fixed: 'right', width: 150, align: 'center', title: '操作', toolbar: '#shenhebar'}

        ]]
        , id: 'contractID'
        , page: true
        , height: '470'
        , limit: '10'
    });

    //刷新待审核table
    function reloadcontractTable() {
        table.reload('contractID',{});
    }
    //初始化时间
    laydate.render({
        elem: '#START_DATE'
        , format: 'yyyy-MM-dd'
    });
    laydate.render({
        elem: '#START_DATEw'
        , format: 'yyyy-MM-dd'
    });
    laydate.render({
        elem: '#START_DATEy'
        , format: 'yyyy-MM-dd'
    });
    laydate.render({
        elem: '#END_DATE'
        , format: 'yyyy-MM-dd'
    });
    laydate.render({
        elem: '#END_DATEw'
        , format: 'yyyy-MM-dd'
    });
    laydate.render({
        elem: '#END_DATEy'
        , format: 'yyyy-MM-dd'
    });
    //查询 待审核的合同
    $('.operateTable .layui-btn').on('click', function () {
        var data = {};
        data.BUILDING_NAME=$("#BUILDING_NAME").val();
        data.ROOM_NUM=$("#ROOM_NUM").val();
        data.COMPANY_NAME=$("#COMPANY_NAME").val();
        data.BUILDING_ID=BUILDING_ID;
        data.BUSINESS_ID=BUSINESS_ID;
        table.reload('contractID', {
            where: data
        });
    });
    //查询 已审核的合同
    $('.operateTable .layui-btn').on('click', function () {
        var data = {};
        data.BUILDING_NAME=$("#BUILDING_NAMEy").val();
        data.ROOM_NUM=$("#ROOM_NUMy").val();
        data.COMPANY_NAME=$("#COMPANY_NAMEy").val();
        data.BUILDING_ID=BUILDING_ID;
        data.BUSINESS_ID=BUSINESS_ID;
        table.reload('YIshenheID', {
            where: data
        });
    });
    //合同待审核 操作
    table.on('tool(shenhef)', function(obj){
        var data = obj.data //获得当前行数据
            ,layEvent = obj.event; //获得 lay-event 对应的值
        var checktype=data.CHECK_STATE;
        if(layEvent === 'shenhe'){
            parent.layer.open({
                zIndex:1000,
                type: 2,    //弹出类型
                title: '查看合同',   //标题
                closeBtn: 1,    //显示关闭按钮
                shade: [0.3],   //遮罩透明度
                area: ['80%', '90%'],  //打开窗口的宽高
                time: 0,    //几秒后自动关闭，默认0时不关闭
                anim: 1,    //动画效果
                resize: false,  //设置成不允许拉伸
                content: '../../../webhtml/contract/contract_info.html?type='+escape(checktype)+'&CONTRACT_ID='+data.id+'&button=shenhe'  //iframe的url
            });
            reloadcontractTable();
            /*layer.confirm('审核是否通过？', {
                btn: ['是','否'] //按钮
            }, function(){
                //审核通过
                $.ajax({
                    url: "/contract/checkcontract",
                    data: {ID: data.id, CHECK_STATE: 1},
                    dataType: "json",
                    type: "post",
                    async: false,
                    success: function (res) {
                        if (res.errcode === "0") {
                        } else if (res.errcode === "1") {
                            layer.msg(res.errmsg, function () {
                            });
                        }
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        console.error(XMLHttpRequest.status);
                        console.error(XMLHttpRequest.readyState);
                        console.error(textStatus);
                    }
                });
                reloadcontractTable();
                layer.msg('审核成功！', {icon: 1});
            }, function(){
                //审核不通过
                layer.msg('为什么不通过？', {
                    time: 20000, //20s后自动关闭
                    btn: ['确定？', '取消？']
                });
                //审核不通过
                $.ajax({
                    url: "/contract/checkcontract",
                    data: {ID: data.id, CHECK_STATE: 2},
                    dataType: "json",
                    type: "post",
                    async: false,
                    success: function (res) {
                        if (res.errcode === "0") {
                        } else if (res.errcode === "1") {
                            layer.msg(res.errmsg, function () {
                            });
                        }
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        console.error(XMLHttpRequest.status);
                        console.error(XMLHttpRequest.readyState);
                        console.error(textStatus);
                    }
                });
                reloadcontractTable();
            });*/
        } else if(layEvent === 'yclbar'){

        }
    });
    //合同已审核 查看 操作
    table.on('tool(yishenhef)', function(obj){
        var data = obj.data //获得当前行数据
            ,layEvent = obj.event; //获得 lay-event 对应的值
        if(layEvent === 'chakan'){
            parent.layer.open({
                zIndex:1000,
                type: 2,    //弹出类型
                title: '查看合同',   //标题
                closeBtn: 1,    //显示关闭按钮
                shade: [0.3],   //遮罩透明度
                area: ['80%', '90%'],  //打开窗口的宽高
                time: 0,    //几秒后自动关闭，默认0时不关闭
                anim: 1,    //动画效果
                resize: false,  //设置成不允许拉伸
                content: '../../../webhtml/contract/contract_info.html?CONTRACT_ID='+data.id+'&button=chakan'  //iframe的url
            });
            reloadcontractTable();

        }
    });
    // 加载待审核  账单
    table.render({
        elem: '#billshenhe'
        , url: '/billController/billshenhe'
        , where:data
        , cols: [[
            {field: 'BUILDING_NAME', event: 'setSign', title: '楼宇名称', width: 160}
            , {field: 'ROOM_NUM', event: 'setSign', title: '项目编号', width: 120}
            , {field: 'BYNAME', event: 'setSign', title: '别名', width: 120}
            , {field: 'COMPANY_NAME', event: 'setSign', title: '承租方', width: 120}
            , {field: 'START_DATE', event: 'setSign', title: '账单周期', width: 200}
            , {field: 'BILL_NAME', event: 'setSign', title: '账单类型', width: 120}
            , {field: 'TOTAL_PRICE', event: 'setSign',align: 'right', title: '账单金额', width: 120}
            , {field: 'PAY_DATE', event: 'setSign', title: '应收租日', width: 120}
            , {field: 'CREATE_ID', event: 'setSign', title: '提交人', width: 120}
            , {field: 'CREATE_DATE', event: 'setSign', title: '提交时间', width: 180}
            , {field: 'INFO', event: 'setSign', title: '备注', width: 120}
            , {fixed: 'right', width: 100, align: 'center',title: '操作', toolbar: '#billshenhebar'}

        ]]
        , id: 'billID'
        , page: true
        , height: '470'
        , limit: '10'
    });
    //查询 待审核的账单
    $('.operateTablews .layui-btn').on('click', function () {
        var data = {};
        data.BYNAME=$("#BYNAMEw").val();
        data.COMPANY_NAME=$("#COMPANY_NAMEw").val();
        data.START_DATE=$("#START_DATEw").val();
        data.END_DATE=$("#END_DATEw").val();
        data.TOTAL_PRICE=$("#TOTAL_PRICEw").val();
        data.BUILDING_ID=BUILDING_ID;
        data.BUSINESS_ID=BUSINESS_ID;
        data.TOTAL_PRICE_STA = $("#TOTAL_PRICEwSTA").val();
        data.TOTAL_PRICE_END = $("#TOTAL_PRICEwEND").val();
        data.BILL_NAME = $("#BILL_NAMEw").val();
        table.reload('billID', {
            where: data
        });
    });
    //待审核 账单操作
    table.on('tool(shenhezdf)', function(obj){
        var data = obj.data //获得当前行数据
            ,layEvent = obj.event; //获得 lay-event 对应的值
        var nametype=data.BILL_NAME;
        //有滞纳金标识
        var LATE_FEE = data.LATE_PRICE;//获取是否有滞纳金
        var payment_logo; // 1  支出  0 收费
        if(!data.TOTAL_PRICE.indexOf("-")){//应付账单标识
            payment_logo=1;
        }
        var type;
        if("租金"==nametype){
            type=0;
        }
        if("押金"==nametype){
            type=1;
        }
        if("水费"==nametype){
            type=2;
        }
        if("电费"==nametype){
            type=3;
        }
        if("燃气费"==nametype){
            type=4;
        }
        if(layEvent === 'shenhezd'){
            parent.layer.open({
                zIndex:1000,
                type: 2,    //弹出类型
                title: '审核账单',   //标题
                closeBtn: 1,    //显示关闭按钮
                shade: [0.3],   //遮罩透明度
                area: ['80%', '90%'],  //打开窗口的宽高
                time: 0,    //几秒后自动关闭，默认0时不关闭
                anim: 1,    //动画效果
                resize: false,  //设置成不允许拉伸
                content: '../../../webhtml/daiban/bill_shenhe.html?payment_logo='+escape(payment_logo)+'&type='+escape(nametype)+'&BILL_ID='+data.ID+'&button=shenhe'  //iframe的url
            });

        } else if(layEvent === 'yclbar'){

        }
    });

    //监听 切换金额搜索 区间 和 等值金额w
    $('.qiehuanw').change(function () {
        if ("0" == $(this).children('option:selected').val()) {
            //固定金额
            $(".qiehuanjinew").html("<div class=\"layui-inline\">\n" +
                "                                <input class=\"layui-input\" name=\"TOTAL_PRICE\" id=\"TOTAL_PRICEw\" placeholder=\"金额\" >\n" +
                "                            </div>");
        }
        if ("1" == $(this).children('option:selected').val()) {
            $(".qiehuanjinew").html("<div class=\"layui-inline jinebox\">\n" +
                "                                    <input class=\"layui-input\" name=\"TOTAL_PRICE\" id=\"TOTAL_PRICEwSTA\" placeholder=\"最小值\" >\n" +
                "                                </div>\n" +
                "                                <div class=\"layui-inline jinebox\">\n" +
                "                                    <input class=\"layui-input\" name=\"TOTAL_PRICE\" id=\"TOTAL_PRICEwEND\" placeholder=\"最大值\" >\n" +
                "                                </div>");
        }
    });
    //加载所有搜索下拉框账单类型值
    $.ajax({
        url: "/property/propertyListALL/",
        dataType: "json",
        type: "post",
        async: false,
        success: function (res) {
            if (res.errcode === "0") {
                for (var i = 0; i < res.data.length; i++) {
                    $("#BILL_NAMEw").append("<option value=" + res.data[i].CODE + ">" + res.data[i].CODE + "</option>");
                }
            } else if (res.errcode === "1") {
                layer.msg(res.errmsg, function () {
                });
            }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            console.error(XMLHttpRequest.status);
            console.error(XMLHttpRequest.readyState);
            console.error(textStatus);
        }
    });

});

//刷新待审核table合同
function reloadcontractTable() {
    table.reload('contractID',{});
}
//刷新待审核账单 table
function reloadBillTable() {
    table.reload('billID',{});
}

